package com.kxf.activitylaunchmode;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.Spinner;
import android.widget.TextView;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import java.util.LinkedHashMap;

/**
 * @ProjectName: AndroidTestDemo
 * @Package: com.kxf.activitylaunchmode
 * @ClassName: BaseActivity
 * @Description: java类作用描述
 * @Author: kuangxuefeng
 * @qq: 1024883177
 * @CreateDate: 2020/5/19 20:06
 */
public class BaseActivity extends AppCompatActivity {

    protected String tag = null;
    private Button btn_sta, btn_sta2, btn_stop, btn_sins, btn_stask;
    private TextView tv_info;
    protected static StringBuffer sb = new StringBuffer();
    private CheckBox cb_newtask, cb_cleartop, cb_singletop;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        tag = this.getLocalClassName();
        Log.e(tag, "===onCreate===");
        final String str = "onCreate==》" + this.getLocalClassName() + "@" + Integer.toHexString(hashCode()) + "; TaskId=" + getTaskId();
        sb.append(str + "\n\r");
    }

    private void initView() {
        btn_sta = findViewById(R.id.btn_sta);
        btn_sta2 = findViewById(R.id.btn_sta2);
        btn_stop = findViewById(R.id.btn_stop);
        btn_sins = findViewById(R.id.btn_sins);
        btn_stask = findViewById(R.id.btn_stask);

        tv_info = findViewById(R.id.tv_info);

        cb_newtask = findViewById(R.id.cb_newtask);
        cb_cleartop = findViewById(R.id.cb_cleartop);
        cb_singletop = findViewById(R.id.cb_singletop);

        btn_sta.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        showInfo("启动 StandardLmActivity");
                        Intent intent = new Intent(BaseActivity.this, StandardLmActivity.class);
                        setLmFlags(intent);
                        startActivity(intent);
                    }
                });
            }
        });
        btn_sta2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        showInfo("启动 StandardLm2Activity");
                        Intent intent = new Intent(BaseActivity.this, StandardLm2Activity.class);
                        setLmFlags(intent);
                        startActivity(intent);
                    }
                });
            }
        });
        btn_stop.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        showInfo("启动 SingleTopLmActivity");
                        Intent intent = new Intent(BaseActivity.this, SingleTopLmActivity.class);
                        setLmFlags(intent);
                        startActivity(intent);
                    }
                });
            }
        });
        btn_sins.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        showInfo("启动 SingleInstanceLmActivity");
                        Intent intent = new Intent(BaseActivity.this, SingleInstanceLmActivity.class);
                        setLmFlags(intent);
                        startActivity(intent);
                    }
                });
            }
        });
        btn_stask.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        showInfo("启动 SingleTaskLmActivity");
                        Intent intent = new Intent(BaseActivity.this, SingleTaskLmActivity.class);
                        setLmFlags(intent);
                        startActivity(intent);
                    }
                });
            }
        });
    }

    @Override
    protected void onResume() {
        super.onResume();
        Log.e(tag, "===onResume===" + this.getLocalClassName() + "@" + Integer.toHexString(hashCode()));
        initView();
        final String str = "onResume==》" + this.getLocalClassName() + "@" + Integer.toHexString(hashCode()) + "; TaskId=" + getTaskId();
        new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    Thread.sleep(500);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                showInfo(str);
            }
        }).start();
    }

    @Override
    protected void onPause() {
        super.onPause();
        Log.e(tag, "===onPause===");
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        showInfo("-onDestroy==》" + this.getLocalClassName() + "@" + Integer.toHexString(hashCode()));
        Log.e(tag, "===onDestroy===" + this.getLocalClassName() + "@" + Integer.toHexString(hashCode()));
    }

    @Override
    protected void onStart() {
        super.onStart();
        Log.e(tag, "===onStart===" + this.getLocalClassName() + "@" + Integer.toHexString(hashCode()) + "; getTaskId()=" + getTaskId());
    }

    protected void showInfo(String str){
        if (!TextUtils.isEmpty(str)){
            sb.append(str + "\n\r");
        }
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                tv_info.setText(sb.toString());
            }
        });
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        switch (keyCode){
            case KeyEvent.KEYCODE_BACK:
                showInfo(this.getLocalClassName() + "@" + Integer.toHexString(hashCode()) + " 按返回");
                break;
        }
        return super.onKeyDown(keyCode, event);
    }

    private void setLmFlags(Intent intent){
        if (cb_newtask.isChecked() || cb_cleartop.isChecked() || cb_singletop.isChecked()){
            int flags = 0;
            String str = "启动标志设置：";
            if (cb_newtask.isChecked()){
                flags = Intent.FLAG_ACTIVITY_NEW_TASK;
                str = str + "newtask ";
            }

            if (cb_cleartop.isChecked()){
                flags = flags | Intent.FLAG_ACTIVITY_CLEAR_TOP;
                str = str + "cleartop ";
            }

            if (cb_singletop.isChecked()){
                flags = flags | Intent.FLAG_ACTIVITY_SINGLE_TOP;
                str = str + "singletop";
            }
            showInfo(str);
            intent.setFlags(flags);
        }
    }
}
